﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Rebus.Messages;
using Rebus.Serialization;

namespace Abp.Rebus.Core
{
        public class StringSerializer : ISerializer
        {
            private readonly Encoding _encoder;
            public StringSerializer()
            {
                _encoder = Encoding.UTF8;
            }

            public StringSerializer(Encoding encoder)
            {
                _encoder = encoder;
            }



            public async Task<TransportMessage> Serialize(Message message)
            {
                var bytes = new byte[0];
                if (message.Body != null)
                {
                    bytes =
                    _encoder.GetBytes(message.Body.ToString());
                }

                return new TransportMessage(message.Headers, bytes);
            }
            public async Task<Message> Deserialize(TransportMessage transportMessage)
            {
                var headers = transportMessage.Headers;
                var body = transportMessage.Body;
                var obj = _encoder.GetString(body);
                var message = new Message(headers, obj);
                return message;
            }
        }
}
